# coding=utf8
from app.extensions import db
from app.modules.common.utils import JsonEncoder
from app.modules.common.dbUtil import createId
from sqlalchemy import func


class PlanTest(db.Model, JsonEncoder):
    __tablename__ = "plan_test"
    id = db.Column(db.String, primary_key = True)
    plan_id = db.Column(db.String)
    plan_name = db.Column(db.String)
    agent_id = db.Column(db.String)
    agent_ip = db.Column(db.String)
    create_time = db.Column(db.DateTime, default = func.now())
    creater_account = db.Column(db.String)
    creater_realname = db.Column(db.String)
    case_names = db.Column(db.Text)
    case_ids = db.Column(db.Text)
    tags = db.Column(db.Text)
    root_case = db.Column(db.String)
    total = db.Column(db.String)
    success = db.Column(db.String)
    fail = db.Column(db.String)
    report_path = db.Column(db.String)
    desc = db.Column(db.String)

    def __init__(self, plan_id = None, plan_name = None, agent_id = None, agent_ip = None, creater_account = None,
                 creater_realname = None, case_names = None, root_case = None, total = None, success = None,
                 fail = None, report_path = None, case_ids = None, tags = None,desc=None):
        self.id = createId('AT')
        self.plan_id = plan_id
        self.plan_name = plan_name
        self.agent_id = agent_id
        self.agent_ip = agent_ip
        self.creater_account = creater_account
        self.creater_realname = creater_realname
        self.case_names = case_names
        self.case_ids = case_ids
        self.root_case = root_case
        self.total = total
        self.success = success
        self.fail = fail
        self.report_path = report_path
        self.tags = tags
        self.desc = desc

    def to_dict(self):
        return {
            'id': self.id,
            'plan_id': self.plan_id,
            'plan_name': self.plan_name,
            'agent_id': self.agent_id,
            'agent_ip': self.agent_ip,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S') if self.create_time else None,
            'creater_account': self.creater_account,
            'creater_realname': self.creater_realname,
            'case_names': self.case_names,
            'case_ids': self.case_ids,
            'tags': self.tags,
            'root_case': self.root_case,
            'total': self.total,
            'success': self.success,
            'fail': self.fail,
            'desc': self.desc,
            'report_path': self.report_path
        }
